package com.sl.biz.modular.process.dao.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sl.biz.modular.process.dao.domain.dto.ProcessCompVersionDTO;
import com.sl.biz.modular.process.dao.domain.entity.ProcessCompVersionDO;
import com.sl.biz.modular.process.dao.mapper.ProcessCompVersionMapper;
import com.sl.biz.modular.process.dao.service.ProcessCompVersionDaoService;
import com.sl.common.enums.CommonDsiableFlagEnum;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class ProcessCompVersionDaoServiceImpl extends ServiceImpl<ProcessCompVersionMapper, ProcessCompVersionDO> implements ProcessCompVersionDaoService {

    @InterceptorIgnore(tenantLine = "true")
    @Override
    public List<ProcessCompVersionDTO> queryEnableList(String orgId, String compCode, String compVersion) {

        LambdaQueryWrapper<ProcessCompVersionDO> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ProcessCompVersionDO::getDisableFlag, CommonDsiableFlagEnum.ENABLE_FLAG.getCode());
        wrapper.eq(StrUtil.isNotBlank(compCode), ProcessCompVersionDO::getCompCode, compCode);
        wrapper.eq(StrUtil.isNotBlank(compVersion), ProcessCompVersionDO::getCompVersion, compVersion);
        wrapper.eq(StrUtil.isNotBlank(orgId), ProcessCompVersionDO::getOrgId, orgId);
        List<ProcessCompVersionDO> list = baseMapper.selectList(wrapper);
        return BeanUtil.copyToList(list, ProcessCompVersionDTO.class);
    }

    @Override
    public Boolean setStatusFlag(ProcessCompVersionDO assertUserUse) {
        LambdaUpdateChainWrapper<ProcessCompVersionDO> chainWrapper = new LambdaUpdateChainWrapper<>(baseMapper);
        chainWrapper.eq(ProcessCompVersionDO::getId, assertUserUse.getId());
        chainWrapper.set(ProcessCompVersionDO::getDisableFlag, assertUserUse.getDisableFlag());
        boolean ret = chainWrapper.update();
        return ret;
    }

    @Override
    public List<ProcessCompVersionDTO> queryList(ProcessCompVersionDO processCompVersionDO) {
        LambdaQueryWrapper<ProcessCompVersionDO> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(StrUtil.isNotBlank(processCompVersionDO.getCompCode()),
                ProcessCompVersionDO::getCompCode, processCompVersionDO.getCompCode());
        List<ProcessCompVersionDO> list = baseMapper.selectList(wrapper);
        return BeanUtil.copyToList(list, ProcessCompVersionDTO.class);
    }

    @Override
    public Boolean removeByCompCode(String compCode) {
        LambdaQueryWrapper<ProcessCompVersionDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProcessCompVersionDO::getCompCode, compCode);
        return super.remove(queryWrapper);
    }
}
